package com.app.twth.ui.b2ctrailer;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2cTrailerBinding;
import com.app.twth.ui.b2ctrailer.submit.B2CTrailerSubmitActivity;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

/**
 * @describe :
 */
public class B2CTrailerActivity extends BaseActivity<ActivityB2cTrailerBinding, B2CTrailerViewModel> {
    private String TAG = getClass().getSimpleName();

    @Override
    protected void initListener() {
        viewModel.b2cTrailerLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2CTrailer", data);
            bundle.putSerializable("data", viewModel.infoMutableLiveData.getValue());
            AppRouter.showActivityForResult(mContext, B2CTrailerSubmitActivity.class, bundle, Constant.ForResult2);

        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Trailer#";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        viewModel.b2cTrailerInitInfo();
        binding.tvCode.requestFocus();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                if (Utils.isEmpty(viewModel.code.getValue())) {
                    ToastUtil.showToast("Please enter the trailer");
                    return;
                }
                viewModel.b2cTrailerEntryTrailer();
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constant.ForResult2:
                if (resultCode == RESULT_OK) {

                    viewModel.b2cTrailerInitInfo();
                    binding.tvCode.setText("");
                    binding.tvCode.requestFocus();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
